﻿using System.Collections.Generic;
using System.Linq;

namespace CodeContrib
{
	public class RandomItem<T> : IRandom<T>
	{
		private readonly IEnumerable<T> _items;
		private readonly RandomInteger _randomInteger;

		public RandomItem(IEnumerable<T> items)
		{
			Guard.NotNullOrEmpty(items, "items");

			_items = items;
			_randomInteger = new RandomInteger(0, _items.Count());
		}

		public T Next()
		{
			return _items.ElementAt(_randomInteger.Next());
		}
	}
}